// import io from './lib/socket.io/weapp.socket.io'
const {
  regeneratorRuntime
} = global
import {
  setLoginUrl
} from './lib/page.auth'

global.regeneratorRuntime = require('./lib/regenerator/runtime-module')

let api = require('/utils/api.js');
let util = require('/utils/util.js');
let http = require('/utils/http.js');
let emojis = require('/emoji/emoji');

App({
  globalData: {
    unionId: 0,
    user: {},
    user_id: '',
    hasUser: false,
    //socket连接
    socketConnect: false,
    //socket连接打开
    socketOpen: false,
    //全局chats记录 chats=[chater_id=>[ ]]
    chats: {},
    //全局聊天对象
    chaters: {},
    timer: 0,
    websocket: null,
    sumUnRead: 0, //未读总记录数
  },

  onLaunch(options) {
    // setLoginUrl(api.login);

    // this.auth();
  },
  onShow(options) {
    // wx.hideTabBar({})
    // this.auth();
    // this.globalData.timer = setInterval(this.keepAlive, 40 * 1000);
  },

  onHide() {

  },

  onError(msg) {
    console.error("[APP ERROR]", msg)
  },


  userInfoReadyCallback: function(res) {
    console.log('获取用户信息回调');
    console.log(res);

  },


  //登录
  login() {
    return new Promise(function(resolve, reject) {
      var that = this;
      console.log("我走login");
      wx.checkSession({

        success: function(res) {
          console.log(res, '======')
          wx.login({
            success: function(res) {
              var code = res.code;
              if (code) {
                http.get(api.login, {
                  code: code
                }, function(res) {
                  if (res.data.code == 1) {
                    console.log("token" + res.data.data);
                    wx.setStorageSync('token', res.data.data);
                    var token = wx.getStorageSync('token');
                    http.get(api.user_info, {
                      token: token
                    }, function(res) {
                      wx.setStorageSync('user_info', res.data);
                      resolve(res);
                      //that.getChaters(res.data.data.id);
                    })
                  } else {
                    wx.setStorageSync('token', '');
                    wx.showToast({
                      title: res.msg,
                      icon: 'none',
                      duration: 2000
                    });
                  }

                }, "登录中...");
              } else {
                wx.showToast({
                  title: '您取消了授权！',
                  icon: 'none',
                  duration: 2000
                });
              }
            },
            fali: function() {
              wx.showToast({
                title: '请检查网络连接！',
                icon: 'none',
                duration: 2000
              });
            }
          });
        },
        fail: function(res) {
          wx.login({
            success: function(res) {
              var code = res.code;
              // console.log(res);
              if (code) {
                http.get(api.login, {
                  code: code
                }, function(res) {
                  if (res.data.code == 1) {
                    wx.setStorageSync('token', res.data.data);
                  } else {
                    wx.setStorageSync('token', '');
                    wx.showToast({
                      title: res.msg,
                      icon: 'none',
                      duration: 2000
                    });
                  }

                }, "请求中...");

                //
              } else {
                wx.showToast({
                  title: '您取消了授权！',
                  icon: 'none',
                  duration: 2000
                });
              }
            },
            fali: function() {
              wx.showToast({
                title: '请检查网络连接！',
                icon: 'none',
                duration: 2000
              });
            }
          });
        }
      })
    })
  },

  login_two() {
    var that = this;
    return new Promise(function(resolve, reject) {
      wx.login({
        success: function(res) {
          var code = res.code;
          if (code) {
            http.get(api.login, {
              code: code
            }, function(res) {
              if (res.data.code == 1) {
                wx.setStorageSync('token', res.data.data);
                var token = wx.getStorageSync('token');
                http.get(api.user_info, {
                  token: token
                }, function(res) {
                  wx.setStorageSync('user_info', res.data);
                  that.globalData.user = res.data.data;
                  that.globalData.hasUser = true;

                  // that.getChaters(res.data.data.id);
                })
              } else {
                wx.setStorageSync('token', '');
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 2000
                });
              }

            }, "登录中...");
          } else {
            wx.showToast({
              title: '您取消了授权！',
              icon: 'none',
              duration: 2000
            });
          }
        },
        fali: function() {
          wx.showToast({
            title: '请检查网络连接！',
            icon: 'none',
            duration: 2000
          });
        }
      });
    })
  }
})